import { Controller, Get, Query } from '@nestjs/common';
import { ReptileService } from './reptile.service';
import { ApiQuery, ApiTags } from '@nestjs/swagger';
import { PageDto } from '../user/dto/page.dto';
import { CommonService } from '../../common/common.service';
import { Auth } from '../../Decorators/Auth';

@ApiTags('reptile')
@Controller('reptile')
export class ReptileController {
  constructor(private readonly reptileService: ReptileService, private commonService: CommonService) {}

  @Get('/list')
  @ApiQuery({ name: 'pageSize', required: false })
  @ApiQuery({ name: 'pageNum', required: false })
  @Auth('画廊列表')
  async getUserList(@Query() page: PageDto) {
    let redisResult = await this.commonService.CacheClient.get('imageCache/' + page.pageNum + '&' + page.pageSize);
    if (redisResult) {
      return JSON.parse(redisResult);
    }
    let [list, total] = await this.reptileService.getReptile(page);

    await this.commonService.CacheClient.set(
      'imageCache/' + page.pageNum + '&' + page.pageSize,
      JSON.stringify({ code: 1, total, list }),
      'EX',
      60 * 60 * 10,
    );
    return {
      code: 1,
      total,
      list,
    };
    // const data = new PageVto<Reptile>();
    // data.pageNum = page.pageNum;
    // data.pageSize = page.pageSize;
    // data.list = result[0];
    // data.total = result[1];
    // return data;
  }
}
